export default {
  toggleItemCheck (state, { id }) {
    state.cart = state.cart.map(shop => {
      if (shop.id === id) shop.check = !shop.check
      return shop
    })
  },
  plusItemCount (state, { id }) {
    state.cart = state.cart.map(shop => {
      if (shop.id === id) shop.count++
      return shop
    })
  },
  minusItemCount (state, { id }) {
    state.cart = state.cart.map(shop => {
      if (shop.id === id) shop.count--
      return shop
    })
  },
  addToCart (state, shopInfo) {
    const isExist = state.cart.some(shop => shop.id === shopInfo.id)
    if (isExist) {
      // 商品已存在
      state.cart = state.cart.map(shop => {
        if (shop.id === shopInfo.id) shop.count++
        return shop
      })
    } else {
      state.cart.push({
        ...shopInfo,
        check: true,
        count: 1
      })
    }
  }
}
